# _*_ coding : utf-8 _*_
# @Time : 2023/12/10 19:12
# @Author : 韩宗森
# @File : 16_代理池
# @Project : pythonProject

import random
import urllib.request

# 代理列表
proxied_pool = [
    {'http': '180.123.9.124:8888'},
    {'http': '58.220.95.55:9400'},
    {'http': '58.220.95.79:10000'},
    {'http': '223.70.126.84:3128'},
    {'http': '121.8.215.106:9797'},
    {'http': '221.226.75.86:55443'},
    {'http': '183.247.152.98:53281'},
    {'http': '60.182.35.230:8888'},
    {'http': '114.231.42.64:8888'},
    {'http': '58.220.95.86:9401'},
    {'http': '122.136.212.132:53281'},
    {'http': '117.26.41.218:8888'}
]

# 随机选择
proxies = random.choice(proxied_pool)

url = 'https://www.baidu.com/s?wd=ip'

headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36'
}

# 请求对象的定制
request = urllib.request.Request(url=url, headers=headers)
# 使用代理
handler = urllib.request.ProxyHandler(proxies=proxies)

opener = urllib.request.build_opener(handler)
# 模拟请求
response = opener.open(request)

# 响应内容
content = response.read().decode('utf-8')

# 保存到本地
with open("daili.html", 'w', encoding='utf-8') as fp:
    fp.write(content)
